package com.example.im.adapter;

import android.widget.ImageView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.example.common.utils.DateUtils;
import com.example.common.utils.DisplayUtils;
import com.example.common.utils.ImgLoader;
import com.example.im.R;
import com.tencent.imsdk.v2.V2TIMConversation;
import com.tencent.imsdk.v2.V2TIMMessage;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.List;

public class ConversationAdapter extends BaseQuickAdapter<V2TIMConversation, BaseViewHolder> {

    public ConversationAdapter(@Nullable List data) {
        super(R.layout.item_main_conversation, data);
    }

    @Override
    protected void convert(@NotNull BaseViewHolder helper, V2TIMConversation v2TIMConversation) {
        helper.setText(R.id.tv_name, v2TIMConversation.getShowName());
        helper.setText(R.id.tv_time, DateUtils.formatLongData(v2TIMConversation.getLastMessage().getTimestamp() * 1000, "MM-dd HH:mm"));
        helper.setGone(R.id.tv_unReadNum, v2TIMConversation.getUnreadCount()!=0 ? false : true);
        helper.setText(R.id.tv_unReadNum, v2TIMConversation.getUnreadCount() + "");
        ImgLoader.loadRoundImg(getContext(), v2TIMConversation.getFaceUrl(), DisplayUtils.dpToPx(12), (ImageView) helper.getView(R.id.iv_photo));
        //展示最后一条消息
        if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_TEXT) {
            helper.setText(R.id.tv_message, v2TIMConversation.getLastMessage().getTextElem().getText());
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_IMAGE) {
            helper.setText(R.id.tv_message, "[图片]");
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_SOUND) {
            helper.setText(R.id.tv_message, "[语音消息]");
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_VIDEO) {
            helper.setText(R.id.tv_message, "[视频消息]");
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_FILE) {
            helper.setText(R.id.tv_message, "[文件]");
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_LOCATION) {
            helper.setText(R.id.tv_message, "[位置]");
        } else if (v2TIMConversation.getLastMessage().getElemType() == V2TIMMessage.V2TIM_ELEM_TYPE_NONE) {
            helper.setText(R.id.tv_message, "");
        } else {
            helper.setText(R.id.tv_message, "新消息");
        }
    }
}
